// pages/addEquipment/addEquipment.js
var app = getApp();
var util = require("../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    contract_no: '', //合同编号
    device_no: '', //设备编号
    id_card: '', //身份证
    equipment: [],
    scanning: '../../images/scanning.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    that.getNumber();
  },

  /**
   * 获取合同编号
   */
  inputchange(e) {
    var that = this;
    that.setData({
      contract_no: e.detail.value
    });
  },
  /**
   * 获取设备编号
   */
  inputdevice_no(e) {
    var that = this;
    that.setData({
      device_no: e.detail.value
    });
  },
  /**
   * 获取身份证
   */
  inputid_card(e) {
    var that = this;
    that.setData({
      id_card: e.detail.value
    });
  },
  //确认事件 设备信息
  affirmfun(e) {
    var that = this;
    var contract_no = e.detail.value.contract_no;
    var device_no = e.detail.value.device_no;
    var id_card = e.detail.value.id_card;
    if (contract_no == '' && device_no == '' && id_card == '') {
      app.showToast('合同编号、设备号、证件号，必须选填至少一项');
      return false;
    }
    var pattern1 = /^[A-Za-z0-9]+$/;
    if (!pattern1.test(device_no) && device_no != "") {
      app.showToast('您输的设备号格式不正确哟~');
      return false;
    }
    if (!pattern1.test(contract_no) && contract_no != "") {
      app.showToast('您输的合同编号格式不正确哟~');
      return false;
    }
    var pattern1 = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
    if (!pattern1.test(id_card) && id_card != "") {
      app.showToast('您输的证件号不正确哟~');
      return false;
    }
    wx.navigateTo({
      url: '../facilitySearchList/facilitySearchList?contract_no=' + contract_no + '&device_no=' + device_no + '&id_card=' + id_card,
    })
    return false;
  },
  //查看设备编号指南
  read_device_no() {
    wx.navigateTo({
      url: '../guidance/guidance',
    })
  },
  // 获取测试设备编号
  getNumber: function() {
    var that = this;
    app.ajax({
      url: 'Common/Test/bindDeviceNo',
      data: {},
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            equipment: res.data.data
          })

        }
      }
    })
  },
  //复制设备编号
  copy(e) {
    var that = this;
    var serialnum = e.currentTarget.dataset.serialnum;
    wx.setClipboardData({
      data: serialnum,
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            that.setData({
              device_no: serialnum
            })
            //console.log('成功',res);
            // setTimeout(function () {
            //   wx.navigateBack({
            //     delta: 1
            //   })
            // }, 800)
          }
        })
      }
    })
  },
  // 扫码
  scanning: function() {
    var that = this;
    app.scanning(function (data) {
      that.setData({
        device_no: data
      })
    })
  },
  //去商城
  goShop() {
    wx.switchTab({
      url: '../shopping/shoppingHome/shoppingHome'
    })
  }
})